package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.help.subform.abstracts.AbstractUserTeamTransferSubForm;
import com.xbongbong.saas.domain.entity.PaymentTaskUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.subform.PaymentTaskTeamEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 回款计划团队es和数据库格式转化
 * @Description
 * @author: shian
 * @date : 2020/12/1
 */
@Component
public class PaymentTaskTeamTransferSubForm extends AbstractUserTeamTransferSubForm {

        private static final Logger LOG = LoggerFactory.getLogger(PaymentTaskTeamTransferSubForm.class);


        @Override
        public PaasFormDataEntity transferTeamToFormData(UserTeamEntity userTeamEntity) {
        PaymentTaskUserEntity paymentTaskUserEntity = (PaymentTaskUserEntity) userTeamEntity;
        PaasFormDataEntity paasFormDataEntity = createPaasFormDataEntityForUserTeam(userTeamEntity);

        JSONObject teamJson = JSONObject.parseObject(JSON.toJSONString(paymentTaskUserEntity));

        JSONObject data = new JSONObject();

        PaymentTaskTeamEnum[] paymentTaskTeamEnums = PaymentTaskTeamEnum.values();
        for (PaymentTaskTeamEnum paymentTaskTeamEnum : paymentTaskTeamEnums) {
            String dbAttr = paymentTaskTeamEnum.getDbAttr();
            String dataAttr = paymentTaskTeamEnum.getDataAttr();
            Integer fieldType = paymentTaskTeamEnum.getFieldType();
            formatDataForEs(teamJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

        @Override
        public UserTeamEntity transferFormDataToTeam(PaasFormDataEntity paasFormDataEntity) {
        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        PaymentTaskTeamEnum[] paymentTaskTeamEnums = PaymentTaskTeamEnum.values();
        for (PaymentTaskTeamEnum paymentTaskTeamEnum : paymentTaskTeamEnums) {
            String dbAttr = paymentTaskTeamEnum.getDbAttr();
            String dataAttr = paymentTaskTeamEnum.getDataAttr();
            Integer fieldType = paymentTaskTeamEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);

        }
        PaymentTaskUserEntity paymentTaskUserEntity = JSON.parseObject(dbData.toJSONString(), PaymentTaskUserEntity.class);
        paymentTaskUserEntity.setAddTime(paasFormDataEntity.getAddTime());
        paymentTaskUserEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        paymentTaskUserEntity.setCorpid(paasFormDataEntity.getCorpid());
        paymentTaskUserEntity.setDel(paasFormDataEntity.getDel());
        paymentTaskUserEntity.setId(paasFormDataEntity.getId());

        return paymentTaskUserEntity;
    }
}

